package com.sky.controller.admin;

import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.SetmealService;
import com.sky.vo.SetmealVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.web.bind.annotation.*;

import java.util.List;
@Slf4j
@Api(tags = "套餐相关接口")
@RestController
@RequestMapping("/admin/setmeal")
public class SetmealController {
    /**
     * 套餐管理新增
     */
    @Autowired
    private SetmealService setmealService;
    //清理缓存
    @CacheEvict(cacheNames = "setmeal",key = "#setmealDTO.categoryId")
    @ApiOperation("套餐新增")
    @PostMapping
    public Result insertSetmeal(@RequestBody SetmealDTO setmealDTO){
        setmealService.insertSetmeal(setmealDTO);
        return Result.success();
    }

    /**
     * 套餐分页查询
     * @param setmealPageQueryDTO
     * @return
     */
    @ApiOperation("套餐分页查询")
    @GetMapping("/page")
    public Result<PageResult> pageSetmeal(SetmealPageQueryDTO setmealPageQueryDTO){
        PageResult pageResult = setmealService.pageSetmeal(setmealPageQueryDTO);
        return Result.success(pageResult);
    }

    /**
     * 批量删除套餐信息
     * @param ids
     * @return
     */
    //@CacheEvict:清楚setmeal下全部缓存数据
    @CacheEvict(cacheNames = "setmeal",allEntries = true)
    @ApiOperation("套餐批量删除")
    @DeleteMapping
    public Result deleteSetmeal(@RequestParam List<Long> ids){
        log.info("删除套餐：{}",ids);
        setmealService.deleteSetmeal(ids);
        return Result.success();
    }

    /**
     * 套餐回显
     * @param id
     * @return
     */
    @ApiOperation("套餐回显")
    @GetMapping("/{id}")
    public Result getById(@PathVariable Long id){
        log.info("回显套餐信息：{}",id);
        SetmealVO setmealVO = setmealService.getById(id);
        return Result.success(setmealVO);
    }
    @CacheEvict(cacheNames = "setmeal",allEntries = true)
    @ApiOperation("套餐修改")
    @PutMapping
    public Result updateSetmeal(@RequestBody SetmealDTO setmealDTO){
        log.info("修改套餐信息：{}",setmealDTO);
        setmealService.updateSetmeal(setmealDTO);
        return Result.success();
    }
    @CacheEvict(cacheNames = "setmeal",allEntries = true)
    @ApiOperation("套餐起售、停售")
    @PostMapping("/status/{status}")
    public Result startOrStop(@PathVariable Integer status, Long id){
        log.info("套餐起售、停售：{}",id);
        setmealService.startOrStop(status,id);
        return Result.success();
    }


}
